﻿function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
function setCookie(c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}
    $.ajax({
        url: '/lxoa/username/',
        type: 'POST',
        dataType: 'json',
        headers: { "X-CSRFToken": getCookie('csrftoken') },
        data: {
        },
        success: function (data) {
            if (data && data.success == '1') {
                $('#usernamespan').html(data.uname);
                if (data.admin == 1) {
                    var uli = $('#userman');
                    if (uli.length == 0) {
                        //$('#mainNav').append('<li><a href="/lxoa/user/" id="userman" class="scroll-link">员工管理</a></li>');
                        $('#sidebarnav').append('<li id="userman"><a class="has-arrow" href="/lxoa/user/" aria-expanded="false"><i class="mdi mdi-bullseye"></i><span class="hide-menu">员工管理</span></a></li>');
                        $('#ulexpense').append('<li id="liapprove"><a href="/lxoa/expenseapprove/">报销审批</a></li><li id="liquery"><a href="/lxoa/expensestatistics/">统计查询</a></li>');
                }
                    if (location.href.indexOf('/user/') > 0) {
                        //$('#userman').parent().addClass('active');
                        $('#userman').addClass('active');
                        $('#userman').find('a').addClass('active');
                    }
                    else if (location.href.indexOf('/expenseapprove/') > 0) {
                        $('#liapprove').addClass('active');
                        $('#liapprove').find('a').addClass('active');
                        $('#liapprove').parent().parent().addClass('active');
                    }
                    else if (location.href.indexOf('/expensestatistics/') > 0) {
                        $('#liquery').addClass('active');
                        $('#liquery').find('a').addClass('active');
                        $('#liquery').parent().parent().addClass('active');
                    }
                   
                }
            } else {
                location.href = '/lxoa/login';
            }
        }
    })
$(document).ready(function () {

    });

function logout() {
    $.ajax({
        url: '/lxoa/userlogout/',
        type: 'POST',
        dataType: 'json',
        headers: { "X-CSRFToken": getCookie('csrftoken') },
        data: {
        },
        success: function (data) {
            if (data && data.success == '1') {
                location.href = '/lxoa/login';
            } else {
                alert('退出异常请重新登陆！');
                location.href = '/lxoa/login';
            }
        }
    })
}